package com.example.mysaat.handle_utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.example.mysaat.R;
//自定义的对焦的图片
@SuppressLint("AppCompatCustomView")
public class FocusImageView extends ImageView {
    private final static String TAG = FocusImageView.class.getSimpleName();
    private static final int NO_ID = -1;
    private int mFocusImage = NO_ID;
    private int mFocusSucceedImg = NO_ID;
    private int mFocusFailImg = NO_ID;
    private Animation animation;
    private Handler handler;

    public FocusImageView(Context context) {
        super(context);
        animation = AnimationUtils.loadAnimation(context, R.anim.focusview_show);
        setVisibility(View.GONE);
        handler = new Handler();
        mFocusImage =  R.mipmap.focus_focusing;
        mFocusSucceedImg = R.mipmap.focus_focused;
        mFocusFailImg = R.mipmap.focus_focus_failed;
    }

    public FocusImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        animation =  AnimationUtils.loadAnimation(context,R.anim.focusview_show);
        handler = new Handler();
        TypedArray type = context.obtainStyledAttributes(attrs,R.styleable.FocusImageView);
        mFocusImage = type.getResourceId(R.styleable.FocusImageView_focus_focusing_id,R.mipmap.focus_focusing);
        mFocusSucceedImg = type.getResourceId(R.styleable.FocusImageView_focus_success_id,R.mipmap.focus_focused);
        mFocusFailImg = type.getResourceId(R.styleable.FocusImageView_focus_focus_failed,R.mipmap.focus_focus_failed);
        type.recycle();// 将数组回收 给其他地方使用

        //聚焦时图片不能为空
        if(mFocusFailImg==NO_ID||mFocusSucceedImg==NO_ID||mFocusImage==NO_ID){
            throw new RuntimeException("mFocusImg,mFocusSucceedImg,mFocusFailedImg is null");
        }
    }

    /**
     * 显示聚焦图案
     * @param point
     */
    public void startFocus(Point point){
        if(mFocusFailImg==NO_ID||mFocusSucceedImg==NO_ID||mFocusImage==NO_ID){
            throw new RuntimeException("image is null");
            //根据触摸的位置显示设置图片的位置
        }

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)getLayoutParams();
//        params.topMargin = point.y - getMeasuredHeight()/2; // 将图片显示上升一半，保证点的中心是图的中心
//        params.leftMargin = point.x - getMeasuredWidth()/2; //
        params.topMargin = point.y + this.getMeasuredHeight()/2;
        params.leftMargin = point.x-this.getMeasuredWidth()/2;
        setLayoutParams(params);
        //设置控件可见，并开始动画
        setVisibility(View.VISIBLE);
        setImageResource(mFocusImage);
        startAnimation(animation);

        //3秒后隐藏View。在此处设置是由于可能由于聚焦事件不能发生
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setVisibility(View.GONE);
            }
        },1000);
    }

    /**
     * 聚焦成功的时候返回的函数
     */
    public  void onFoucusSuccess(){
        setImageResource(mFocusSucceedImg);
        //移除在startFocus中设置的callBack,1秒后隐藏改控件
        handler.removeCallbacks(null,null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setVisibility(View.GONE);
            }
        },1000);
    }

    public void onFocusFailed(){
        setImageResource(mFocusFailImg);
        //移除在startFocus中设置的callBack,1秒后隐藏改控件
        handler.removeCallbacks(null,null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setVisibility(View.GONE);
            }
        },1000);
    }

    /**
     * 设置开始聚焦的图片
     * @param focus
     */
    public void setmFocusImage(int focus){
        this.mFocusImage = focus;
    }

    /**
     * 设置聚焦成功的图片
     * @param focus
     */
    public void setmFocusSucceedImg(int focus){
        this.mFocusSucceedImg = focus;
    }

    public FocusImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
